package mediator;

import java.util.Observable;
import java.util.Observer;

import entity.EEntity;
import foundation.FDataStreamReader;

public class MDataStreamHandler extends Observable implements Observer {
	private EEntity eEntity;
	private FDataStreamReader fDataStreamReader;
	private MFileCommunication mFileCommunication;
	
	public MDataStreamHandler(EEntity eEntity, 
			MFileCommunication mFileCommunication, String host, int port) {
		this.eEntity = eEntity;
		this.mFileCommunication = mFileCommunication;
		fDataStreamReader = new FDataStreamReader(host, port);
		fDataStreamReader.addObserver(this);
	}
	
	public void startStreaming() {
		(new Thread(fDataStreamReader)).start();
	}
	
	public void addStatusObserver(Observer obs) {
		fDataStreamReader.addStatusObserver(obs);
	}
	
	public void deleteStatusObserver(Observer obs) {
		fDataStreamReader.deleteStatusObserver(obs);
	}

	@Override
	public void update(Observable obs, Object data) {
		if (data instanceof byte[]) {
			eEntity.updateRegisterData((byte[]) data);
			
			setChanged();
			notifyObservers();
		} else if (data instanceof String) {
			mFileCommunication.addError((String) data);
		}
	}
	
}
